#include "stdafx.h"
#include "Skydome.h"
#include "Globals.h"


Skydome::Skydome() : cObject()
{
}

void Skydome::Init(char *szFileNFG,
				   char *szPosXFile,
				   char *szNegXFile,
				   char *szPosYFile,
				   char *szNegYFile,
				   char *szPosZFile,
				   char *szNegZFile)
{
	char *szTGAFileName[6];
	szTGAFileName[0] = szPosXFile;
	szTGAFileName[1] = szNegXFile;
	szTGAFileName[2] = szPosYFile;
	szTGAFileName[3] = szNegYFile;
	szTGAFileName[4] = szPosZFile;
	szTGAFileName[5] = szNegZFile;
	cObject::Init(GL_TEXTURE_CUBE_MAP, szTGAFileName, 6, szFileNFG);

	//Create model shader
	m_Shader = SkydomeShader();
	m_Shader.Init("../Resources/Shaders/skydomeShaderVS.vs","../Resources/Shaders/skydomeShaderFS.fs");
}

void Skydome::Draw(ESContext *esContext)
{
	glUseProgram(m_Shader.program);
	if(m_Shader.VertexPosition != -1)
	{
		glBindBuffer(GL_ARRAY_BUFFER, m_Model->GetVertexID());
		glEnableVertexAttribArray(m_Shader.VertexPosition);
		glVertexAttribPointer(m_Shader.VertexPosition, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), 0);
		glBindBuffer(GL_ARRAY_BUFFER, 0);
	}

	if(m_Shader.Texture != -1)
	{
		glActiveTexture(GL_TEXTURE0);
		glBindTexture(GL_TEXTURE_CUBE_MAP, m_Texture->GetTextureID()[0]);
		glUniform1i(m_Shader.Texture, 0);
	}

	if(m_Shader.WVPMatrix != -1)
	{
		Matrix maView = Globals::MainCamera->GetVPMatrix();
		glUniformMatrix4fv(m_Shader.WVPMatrix, 1, GL_FALSE, (GLfloat*)&(m_maLocalW * maView).m[0][0]);
	}

	glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_Model->GetIndiceID());
	glDrawElements(GL_TRIANGLES, m_Model->GetIndiceNumber(), GL_UNSIGNED_INT, 0);
	//glDrawElements(GL_LINE_LOOP, m_uiNumIndex, GL_UNSIGNED_INT, 0);
	glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
}

Skydome::~Skydome()
{
	SAFE_DEL(m_Texture);
}